package fabric

import (
	"fmt"
	"strings"
	"time"

	"jihe.common/protos/base_server/proto"
	"jihe.common/tool"
)

func (s *Model) Create(in *proto.BaseFabric) (ret *proto.BaseNull, err error) {
	ret = new(proto.BaseNull)
	in.Id = fmt.Sprintf("%d", tool.GetSnowflakeId())
	in.CreatedAt = time.Now().Format("2006-01-02 15:04:05")
	in.UpdatedAt = time.Now().Format("2006-01-02 15:04:05")
	sqlStr := fmt.Sprintf("INSERT INTO %s (id, name, weave, width, needle, discount, material, count, g, warm_num, line, remark, url, created_at, updated_at)VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15)", s.Name)
	_, err = s.Db.Exec(sqlStr, in.Id, in.Name, in.Weave, in.Width, in.Needle, in.Discount, in.Material, in.Count, in.G, in.WarmNum, in.Line, in.Remark, in.Url, in.CreatedAt, in.UpdatedAt)
	if err != nil {
		return
	}
	return
}

func (s *Model) Update(in *proto.BaseFabric) (ret *proto.BaseNull, err error) {
	ret = new(proto.BaseNull)
	sqlStr := fmt.Sprintf("UPDATE %s SET name = $1, weave = $2, width = $3, needle = $4, discount = $5, material = $6, count = $7, g = $8, warm_num = $9, line = $10, remark = $11, url = $12, updated_at = $13 WHERE id = $14", s.Name)
	_, err = s.Db.Exec(sqlStr, in.Name, in.Weave, in.Width, in.Needle, in.Discount, in.Material, in.Count, in.G, in.WarmNum, in.Line, in.Remark, in.Url, time.Now().Format("2006-01-02 15:04:05"), in.Id)
	if err != nil {
		return
	}
	return
}

func (s *Model) GetList(in *proto.BaseGetListFabricReq) (ret *proto.BaseGetListFabricRes, err error) {
	ret = new(proto.BaseGetListFabricRes)
	sqlStr := fmt.Sprintf("SELECT id, name, weave, width, needle, discount, material, count, g, warm_num, line, remark, url, created_at, updated_at FROM %s ORDER BY created_at DESC LIMIT 100", s.Name)
	rows, e := s.Db.Query(sqlStr)
	if e != nil {
		err = e
		return
	}
	defer rows.Close()
	for rows.Next() {
		item := &proto.BaseFabric{}
		var (
			createdAt *time.Time
			updatedAt *time.Time
		)
		err = rows.Scan(&item.Id, &item.Name, &item.Weave, &item.Width, &item.Needle, &item.Discount, &item.Material, &item.Count, &item.G, &item.WarmNum, &item.Line, &item.Remark, &item.Url, &createdAt, &updatedAt)
		if err != nil {
			return
		}
		if createdAt != nil {
			item.CreatedAt = createdAt.Format("2006-01-02 15:04:05")
		}
		if updatedAt != nil {
			item.UpdatedAt = updatedAt.Format("2006-01-02 15:04:05")
		}
		ret.BaseFabricList = append(ret.BaseFabricList, item)
	}
	return
}

func (s *Model) GetDetail(id string) (ret *proto.BaseFabric, err error) {
	ret = new(proto.BaseFabric)
	sqlStr := fmt.Sprintf("SELECT id, name, weave, width, needle, discount, material, count, g, warm_num, line, remark, url, created_at, updated_at FROM %s WHERE id = $1", s.Name)
	var (
		createdAt *time.Time
		updatedAt *time.Time
	)
	err = s.Db.QueryRow(sqlStr, id).Scan(&ret.Id, &ret.Name, &ret.Weave, &ret.Width, &ret.Needle, &ret.Discount, &ret.Material, &ret.Count, &ret.G, &ret.WarmNum, &ret.Line, &ret.Remark, &ret.Url, &createdAt, &updatedAt)
	if err != nil {
		return
	}
	if createdAt != nil {
		ret.CreatedAt = createdAt.Format("2006-01-02 15:04:05")
	}
	if updatedAt != nil {
		ret.UpdatedAt = updatedAt.Format("2006-01-02 15:04:05")
	}
	return
}

func (s *Model) Delete(req *proto.BaseFabric) (err error) {
	sqlStr := fmt.Sprintf("DELETE FROM %s where id = $1", s.Name)
	_, err = s.Db.Exec(sqlStr, req.Id)
	if err != nil {
		return
	}
	return
}

func (s *Model) GetListByIds(fabricIds []string) (ret []*proto.BaseFabric, err error) {
	args := make([]string, len(fabricIds))
	vals := make([]interface{}, len(fabricIds))
	for k, v := range fabricIds {
		args[k] = fmt.Sprintf("$%d", k+1)
		vals[k] = v
	}
	sqlStr := fmt.Sprintf("SELECT id, name, weave, width, needle, discount, material, count, g, warm_num, line, remark, url, created_at, updated_at FROM %s where id in ("+strings.Join(args, ",")+") ORDER BY created_at DESC LIMIT 100", s.Name)
	rows, e := s.Db.Query(sqlStr, vals...)
	if e != nil {
		err = e
		return
	}
	defer rows.Close()
	for rows.Next() {
		item := &proto.BaseFabric{}
		var (
			createdAt *time.Time
			updatedAt *time.Time
		)
		err = rows.Scan(&item.Id, &item.Name, &item.Weave, &item.Width, &item.Needle, &item.Discount, &item.Material, &item.Count, &item.G, &item.WarmNum, &item.Line, &item.Remark, &item.Url, &createdAt, &updatedAt)
		if err != nil {
			return
		}
		if createdAt != nil {
			item.CreatedAt = createdAt.Format("2006-01-02 15:04:05")
		}
		if updatedAt != nil {
			item.UpdatedAt = updatedAt.Format("2006-01-02 15:04:05")
		}
		ret = append(ret, item)
	}
	return
}
